<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('商品信息列表')" />
    <th:block th:include="include :: select2-css" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <input type="hidden" id="orderId" th:value="${orderId}">
                    <div class="select-list col-sm-12" id="selectType">
                        <ul>
                            <li>
                                <label>选分类：</label>
                                <select name="typeId" class="typeId form-control" onchange="typeChange()">
                                    <option value="">全部</option>
                                </select>
                            </li>
                            <li>
                                <label>选品牌：</label>
                                <select name="brandId" class="brandId form-control" onchange="$.table.search()">
                                    <option value="">全部</option>
                                </select>
                            </li>
                        </ul>
                    </div>
                    <div class="select-list col-sm-12" id="selectOther">
                        <ul>
                            <li>
                                <label>找商品：</label>
                                <input type="text" name="productName" onkeyup="$.table.search()"/>
                            </li>
                            <li>
                                <label>拼音找：</label>
                                <input type="text" name="productShortName" onkeyup="$.table.search()"/>
                            </li>
                            <li>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="location.reload()">
                                    <i class="fa fa-refresh"></i>&nbsp;全部
                                </a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-primary" onclick="selectProducts()">
                    <i class="fa fa-check"></i> 选  中
                </a>
                <label>&nbsp;</label>
                <a class="btn btn-success" onclick="$.operate.add()">
                    <i class="fa fa-plus"></i> 新增商品
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: select2-js" />
    <th:block th:include="include :: jquery-cxselect-js" />
    <script th:inline="javascript">
        var prefix = ctx + "baseInfo/product";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                clickToSelect: true,
                height: 500,
                modalName: "商品信息",
                onClickCell: onClickCell,
                columns: [{
                    checkbox: true
                },
                {
                    field: 'productId',
                    title: '商品id',
                    visible: false
                },
                {
                    field: 'productName',
                    title: '商品名称'
                },
                {
                    field: 'productBrand',
                    title: '品牌'
                },
                {
                    field: 'productUnit',
                    title: '单位'
                },
                {
                    field: 'productPrice',
                    title: '单价'
                },
                {
                    field: 'productImg',
                    title: '图片',
                    formatter: function(value, row, index) {
                        return $.table.imageView(row.productImg);
                    }
                },
                {
                    field: 'purchasePrice',
                    title: '编号'
                },
                {
                    field: 'productStore',
                    title: '库存'
                },
                {
                    field: 'productShortName',
                    title: '简称'
                }]
            };
            $.table.init(options);
        });

        //品牌
        var brandData = [[${brandList}]];
        $('#selectOther').cxSelect({
            required: false,
            firstTitle: '全部',
            selects: ['brandId'],
            jsonName: 'name',
            jsonValue: 'id',
            data: brandData
        });

        //大分类
        var typeParentData = [[${parentType}]];
        $('#selectType').cxSelect({
            required: false,
            firstTitle: '全部',
            selects: ['typeId'],
            jsonName: 'name',
            jsonValue: 'id',
            data: typeParentData
        });

        //小分类选中
        function typeChange() {
            var typeId = $(".typeId").val();
            $.form.reset();
            $.ajax({
                type: "post",
                url: prefix + "/parentTypeChange",
                data: {
                    "typeId": typeId
                },
                success: function(result) {
                    if (result.code == 0) {
                        $('#selectType').cxSelect({
                            required: false,
                            firstTitle: '全部',
                            selects: ['brandId'],
                            jsonName: 'name',
                            jsonValue: 'id',
                            data: result.data.brandList
                        });
                        $('#selectType').cxSelect({
                            required: false,
                            firstTitle: '全部',
                            selects: ['sizeId'],
                            jsonName: 'name',
                            jsonValue: 'id',
                            data: result.data.sizeList
                        });
                    }
                }
            })
            $(".typeId").val(typeId);
            $.table.search();
        }

        //单击选中商品
        function onClickCell(field, value, row, $element){
            if(field != 'productName' && field != 'productImg' && field != '0'){
                //输入数量
                layer.prompt({title: row.productName + '  输入数量', formType: 2}, function(productNum, index){
                    layer.close(index);
                    $.ajax({
                        type: "post",
                        url: ctx + "order/detail/add",
                        data: {
                            "orderId": $("#orderId").val(),
                            "productId": row.productId,
                            "productUnit": row.productUnit,
                            "productPrice": row.productPrice,
                            "productNum": productNum
                        },
                        success: function(result) {
                            if (result.code == 0) {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            }
                        }
                    });
                });
            }
        }

        //多选选中
        var selectLength = [[${selectLength}]];
        function selectProducts() {
            if($.table.selectColumns('productId').length > selectLength){
                $.modal.alertError("本单最多再增加【 " + selectLength + " 】个商品，当前选中了 " + $.table.selectColumns('productId').length + " 个");
                return false;
            }
            var selectData = $("#bootstrap-table").bootstrapTable('getSelections', function (row) {
                return row;
            });
            $.ajax({
                type: "post",
                url: ctx + "order/detail/add",
                data: {
                    "productData":JSON.stringify(selectData),
                    "orderId": $("#orderId").val()
                },
                success: function(result) {
                    if (result.code == 0) {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    }
                }
            });
        }
    </script>
</body>
</html>